﻿using StarPizza.Model.BaseModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.ViewModel.Core
{
    public abstract class IngredientVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public IngredientVM(Ingredient ingredient)
        {
            this.ingredient = ingredient;
            this.Price = ingredient.Price;
            this.Cost = ingredient.Cost;
            ingredient.PropertyChanged += ingredient_PropertyChanged;
        }

        void ingredient_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Cost":
                    this.Cost = ingredient.Cost;
                    break;
                case "Price":
                    this.Price = ingredient.Price;
                    break;
                default:
                    OnPropertyChanged(e.PropertyName);
                    break;
            }
        }
        Ingredient ingredient;

        private int cost;
        public int Cost
        {
            get { return cost; }
            set
            {
                if (value != cost)
                {
                    cost = value;
                    OnPropertyChanged("Cost");
                }
            }
        }

        private int price;
        public int Price
        {
            get { return price; }
            set
            {
                if (value != price)
                {
                    price = value;
                    OnPropertyChanged("Price");
                }
            }
        }

        private int amount;
        public int Amount
        {
            get { return amount; }
            set
            {
                if (value != amount)
                {
                    amount = value;
                    OnPropertyChanged("Amount");
                }
            }
        }

        public virtual void UpdateModel()
        {
            ingredient.Price = this.Price;
            ingredient.Cost = this.Cost;
        }
    }
}
